﻿using System;
using System.Collections;
using System.Collections.Generic;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKArchMethodLibrary.Utils;
using YJKArchMethodLibrary.Utils.Generics;

namespace BuildingOutLine.BuildingOutLine.LoopSearch
{
	public class StructureLoopSearcher
	{
		public StructureLoopSearcher(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.m_Doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_uiApp = cmdData.Application;
		}

		public List<List<Curve>> GetFullFloorEdgeLoops(List<Pole2D> poleList, StructureLoopSearcher.SearchEdgeLocation edgeLocation = StructureLoopSearcher.SearchEdgeLocation.SEL_Outside, bool showTempProfile = false)
		{
			List<List<Curve>> list = new List<List<Curve>>();
			List<List<Curve>> result;
			try
			{
				if (poleList.Count == 0)
				{
					result = list;
				}
				else
				{
					List<Pole2D> poleList2 = this.SegmentPoleList(poleList);
					List<Pole2D> poleList3 = this.FilterOverlapPoleList(poleList2);
					List<Node2D> theNodes = new List<Node2D>();
					this.InitData(poleList3, Node2D.SearchType._Anticlockwise, ref theNodes);
					this.MergeEdge(ref theNodes);
					this.ShowPoleInfos(theNodes);
					this.RecursiveDeleteSingleNode(ref theNodes);
					List<List<Pole2D>> outPoleList = new List<List<Pole2D>>();
					if (!this.SearchOuterBoundary(theNodes, ref outPoleList))
					{
						result = list;
					}
					else
					{
						List<List<Pole2D>> outPoleList2 = new List<List<Pole2D>>();
						this.FormatPoleLoops(outPoleList, ref outPoleList2);
						this.GetLoopCurves(outPoleList2, edgeLocation, ref list);
						result = list;
					}
				}
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				result = list;
			}
			return result;
		}

		public List<List<Curve>> GetCellsEdgeLoops(List<Pole2D> poleList)
		{
			List<List<Curve>> list = new List<List<Curve>>();
			List<List<Curve>> result;
			try
			{
				if (poleList.Count == 0)
				{
					result = list;
				}
				else
				{
					List<Pole2D> poleList2 = this.SegmentPoleList(poleList);
					List<Pole2D> poleList3 = this.FilterOverlapPoleList(poleList2);
					List<Node2D> theNodes = new List<Node2D>();
					this.InitData(poleList3, Node2D.SearchType._Clockwise, ref theNodes);
					this.MergeEdge(ref theNodes);
					this.RecursiveDeleteSingleNode(ref theNodes);
					List<List<Pole2D>> outPoleList = new List<List<Pole2D>>();
					if (!this.SearchCells(theNodes, ref outPoleList))
					{
						result = list;
					}
					else
					{
						List<List<Pole2D>> outPoleList2 = new List<List<Pole2D>>();
						this.FormatPoleLoops(outPoleList, ref outPoleList2);
						this.GetLoopCurves(outPoleList2, StructureLoopSearcher.SearchEdgeLocation.SEL_Center, ref list);
						result = list;
					}
				}
			}
			catch (Exception)
			{
				result = list;
			}
			return result;
		}

		private void ShowPoleInfos(List<Node2D> theNodes)
		{
		}

		private List<Pole2D> SegmentPoleList(List<Pole2D> poleList)
		{
			List<Pole2D> list = new List<Pole2D>();
			List<List<XYZ>> list2 = new List<List<XYZ>>();
			List<Curve> list3 = new List<Curve>();
			foreach (Pole2D pole2D in poleList)
			{
				list2.Add(new List<XYZ>
				{
					pole2D.StartPoint,
					pole2D.EndPoint
				});
				XYZ startPoint = pole2D.StartPoint;
				XYZ endPoint = pole2D.EndPoint;
				Curve item;
				if (pole2D.IsArcPole)
				{
					XYZ xyz = XYZ.BasisZ;
					if (Geometry.LessThan(pole2D.Bulge, 0.0))
					{
						xyz = -xyz;
					}
					XYZ center = pole2D.Center;
					XYZ xyz2 = Geometry.CalculatMidPoint(startPoint, endPoint, center, xyz);
					item = Arc.Create(startPoint, endPoint, xyz2);
				}
				else
				{
					item = Line.CreateBound(startPoint, endPoint);
				}
				list3.Add(item);
			}
			for (int i = 0; i < list3.Count; i++)
			{
				Curve curve = list3[i];
				for (int j = i + 1; j < list3.Count; j++)
				{
					Curve curve2 = list3[j];
					try
					{
						IntersectionResultArray intersectionResultArray;
						SetComparisonResult setComparisonResult = curve.Intersect(curve2, out intersectionResultArray);
						if (setComparisonResult == SetComparisonResult.Overlap)
						{
							IEnumerator enumerator2 = intersectionResultArray.GetEnumerator();
							{
								while (enumerator2.MoveNext())
								{
									object obj = enumerator2.Current;
									IntersectionResult intersectionResult = (IntersectionResult)obj;
									if (intersectionResult.XYZPoint != null)
									{
										list2[i].Add(intersectionResult.XYZPoint);
										list2[j].Add(intersectionResult.XYZPoint);
									}
								}
								goto IL_280;
							}
						}
						if (Geometry.IsEqual(curve.Distance(curve2.GetEndPoint(0)), 0.0, 0.001))
						{
							list2[i].Add(curve2.GetEndPoint(0));
						}
						if (Geometry.IsEqual(curve.Distance(curve2.GetEndPoint(1)), 0.0, 0.001))
						{
							list2[i].Add(curve2.GetEndPoint(1));
						}
						if (Geometry.IsEqual(curve2.Distance(curve.GetEndPoint(0)), 0.0, 0.001))
						{
							list2[j].Add(curve.GetEndPoint(0));
						}
						if (Geometry.IsEqual(curve2.Distance(curve.GetEndPoint(1)), 0.0, 0.001))
						{
							list2[j].Add(curve.GetEndPoint(1));
						}
						IL_280:;
					}
					catch (Exception)
					{
					}
				}
			}
			for (int i = 0; i < poleList.Count; i++)
			{
				Pole2D pole2D2 = poleList[i];
				Curve curve3 = list3[i];
				List<XYZ> intersectPoints = list2[i];
				List<XYZ> list4 = Common.UniqueAndSortIntersectPoints(curve3, intersectPoints);
				if (list4.Count >= 2)
				{
					if (list4.Count == 2)
					{
						list.Add(pole2D2);
					}
					else
					{
						double positiveHalfWidth = pole2D2.PositiveHalfWidth;
						double negativeHalfWidth = pole2D2.NegativeHalfWidth;
						XYZ center2 = pole2D2.Center;
						for (int k = 0; k < list4.Count - 1; k++)
						{
							XYZ xyz3 = list4[k];
							XYZ xyz4 = list4[k + 1];
							if (!Geometry.LessThan(xyz3.DistanceTo(xyz4), 0.0001))
							{
								if (pole2D2.IsArcPole)
								{
									Pole2D item2 = Pole2D.NewStructure2DPole(pole2D2.ComponentId, xyz3, xyz4, center2, XYZ.BasisZ, positiveHalfWidth, negativeHalfWidth);
									list.Add(item2);
								}
								else
								{
									Pole2D item3 = Pole2D.NewStructure2DPole(pole2D2.ComponentId, xyz3, xyz4, positiveHalfWidth, negativeHalfWidth);
									list.Add(item3);
								}
							}
						}
					}
				}
			}
			return list;
		}

		private void MergeEdge(ref List<Node2D> theNodes)
		{
			Node2D leftBottomNode = this.GetLeftBottomNode(theNodes);
			for (int i = 0; i < theNodes.Count; i++)
			{
				Node2D node2D = theNodes[i];
				if (node2D.PoleList.Count == 2 && !Geometry.IsEqual(leftBottomNode.Position, node2D.Position))
				{
					Pole2D newPole = null;
					if (Pole2D.MergeTwoPole(node2D.PoleList[0], node2D.PoleList[1], ref newPole))
					{
						this.UpdateLinkedPole(node2D, node2D.PoleList[0], theNodes, newPole);
						this.UpdateLinkedPole(node2D, node2D.PoleList[1], theNodes, newPole);
						theNodes.RemoveAt(i);
						i--;
					}
				}
			}
		}

		private void UpdateLinkedPole(Node2D curNode, Pole2D curPole, List<Node2D> theNodes, Pole2D newPole)
		{
			XYZ pos = curPole.CurEndPoint(curNode.Position);
			Node2D node2D = this.FindNodeByPos(theNodes, pos);
			int num = node2D.FindLinkPoleWithOtherNode(curNode.Position, Node2D.SearchType._Anticlockwise);
			if (-1 != num)
			{
				node2D.PoleList[num] = newPole;
			}
		}

		private void GetOtherCurves(List<List<Pole2D>> externalEdgeList, List<Pole2D> inputPoleList, ref List<Curve> interiorCurves)
		{
			List<ElementId> list = new List<ElementId>();
			foreach (List<Pole2D> list2 in externalEdgeList)
			{
				foreach (Pole2D pole2D in list2)
				{
					list.Add(pole2D.ComponentId);
				}
			}
			List<Pole2D> list3 = new List<Pole2D>();
			using (List<Pole2D>.Enumerator enumerator3 = inputPoleList.GetEnumerator())
			{
				while (enumerator3.MoveNext())
				{
					Pole2D inputPole = enumerator3.Current;
					int num = list.FindIndex((ElementId s) => s.IntegerValue == inputPole.ComponentId.IntegerValue);
					if (num < 0)
					{
						list3.Add(inputPole);
					}
				}
			}
			this.GetLoopCurves(list3, StructureLoopSearcher.SearchEdgeLocation.SEL_Center, ref interiorCurves);
		}

		private List<Pole2D> FilterOverlapPoleList(List<Pole2D> poleList)
		{
			List<Pole2D> list = new List<Pole2D>();
			list.AddRange(poleList);
			for (int i = 0; i < list.Count; i++)
			{
				for (int j = i + 1; j < list.Count; j++)
				{
					if (list[i].SameAs(list[j]))
					{
						list.RemoveAt(j);
						j--;
					}
				}
			}
			return list;
		}

		private void InitData(List<Pole2D> poleList, Node2D.SearchType searchType, ref List<Node2D> theNodes)
		{
			Dictionary<XYZ, Node2D> dictionary = new Dictionary<XYZ, Node2D>(new XYZComparer());
			foreach (Pole2D pole2D in poleList)
			{
				XYZ xyz = pole2D.StartPoint;
				if (!dictionary.ContainsKey(xyz))
				{
					Node2D value = new Node2D(xyz, new List<Pole2D>
					{
						pole2D
					});
					dictionary.Add(xyz, value);
				}
				else
				{
					Node2D node2D = dictionary[xyz];
					node2D.AddPole(pole2D);
				}
				xyz = pole2D.EndPoint;
				if (!dictionary.ContainsKey(xyz))
				{
					Node2D value2 = new Node2D(xyz, new List<Pole2D>
					{
						pole2D
					});
					dictionary.Add(xyz, value2);
				}
				else
				{
					Node2D node2D2 = dictionary[xyz];
					node2D2.AddPole(pole2D);
				}
			}
			foreach (KeyValuePair<XYZ, Node2D> keyValuePair in dictionary)
			{
				if (searchType == Node2D.SearchType._Anticlockwise)
				{
					keyValuePair.Value.AnticlockwiseSortElements();
				}
				else
				{
					keyValuePair.Value.ClockwiseSortElements();
				}
				theNodes.Add(keyValuePair.Value);
			}
		}

		private void TrimAllNode(ref List<Node2D> theNodes)
		{
			foreach (Node2D node2D in theNodes)
			{
				int count = node2D.PoleList.Count;
				if (count != 1)
				{
					node2D.AnticlockwiseSortElements();
					List<Pole2D> poleList = node2D.PoleList;
					for (int i = 0; i < count; i++)
					{
						int index = i;
						int index2 = i + 1;
						if (i == count - 1)
						{
							index2 = 0;
						}
						Pole2D pole2D = poleList[index];
						Pole2D pole2D2 = poleList[index2];
						double dSAngle = 0.0;
						double dEAngle = 0.0;
						pole2D.GetAngle(node2D.Position, ref dSAngle);
						pole2D2.GetAngle(node2D.Position, ref dEAngle);
						double val = Geometry.BetweenTheAngles(dSAngle, dEAngle, true);
						if (!Geometry.IsEqual(val, Math.PI))
						{
							this.TrimPoleSideline(node2D.Position, ref pole2D, ref pole2D2);
						}
					}
				}
			}
		}

		private void TrimPoleSideline(XYZ position, ref Pole2D pole1, ref Pole2D pole2)
		{
			XYZ xyz = pole1.CurNegativeStartPoint(position);
			XYZ xyz2 = pole1.CurNegativeEndPoint(position);
			if (Geometry.IsEqual(xyz, xyz2))
			{
				return;
			}
			Curve curve;
			if (pole1.IsArcPole)
			{
				XYZ center = pole1.Center;
				XYZ xyz3 = Geometry.CalculatMidPoint(xyz, xyz2, center, XYZ.BasisZ);
				curve = Arc.Create(xyz, xyz2, xyz3);
			}
			else
			{
				curve = Line.CreateBound(xyz, xyz2);
			}
			XYZ xyz4 = pole2.CurPositiveStartPoint(position);
			XYZ xyz5 = pole2.CurPositiveEndPoint(position);
			if (Geometry.IsEqual(xyz4, xyz5))
			{
				return;
			}
			Curve curve2;
			if (pole2.IsArcPole)
			{
				XYZ center2 = pole2.Center;
				XYZ xyz6 = Geometry.CalculatMidPoint(xyz4, xyz5, center2, XYZ.BasisZ);
				curve2 = Arc.Create(xyz4, xyz5, xyz6);
			}
			else
			{
				curve2 = Line.CreateBound(xyz4, xyz5);
			}
			XYZ xyz7 = Geometry.IntersectWithTwoCurves(curve, curve2, position, true, true, this.m_uiApp.Application);
			if (xyz7 != null)
			{
				pole1.SetCurNegativeStartPoint(position, xyz7);
				pole2.SetCurPositiveStartPoint(position, xyz7);
			}
		}

		private bool SearchOuterBoundary(List<Node2D> theNodes, ref List<List<Pole2D>> outPoleList)
		{
			int num = -1;
			int num2 = 0;
			int num3 = 0;
			int num4 = theNodes.Count * 4;
			bool isFirstPt = true;
			List<XYZ> list = new List<XYZ>();
			List<double> list2 = new List<double>();
			List<Pole2D> list3 = new List<Pole2D>();
			Node2D node2D = null;
			Node2D prevNode = null;
			while (theNodes.Count > 1 && num2 < 2 && num3 < num4)
			{
				if (num == -1 || num == 1)
				{
					list = new List<XYZ>();
					list2 = new List<double>();
					list3 = new List<Pole2D>();
					Node2D leftBottomNode = this.GetLeftBottomNode(theNodes);
					if (leftBottomNode == null)
					{
						return false;
					}
					node2D = leftBottomNode;
					list.Add(node2D.Position);
					num3++;
					prevNode = null;
					isFirstPt = true;
				}
				Pole2D pole2D = null;
				Node2D nextNode = this.GetNextNode(isFirstPt, prevNode, node2D, theNodes, Node2D.SearchType._Anticlockwise, ref pole2D);
				if (nextNode != null)
				{
					double item = pole2D.CurBulge(node2D.Position);
					list2.Add(item);
					list3.Add(pole2D);
					prevNode = node2D;
					isFirstPt = false;
					node2D = nextNode;
					list.Add(node2D.Position);
					num3++;
					if (this.IsClosed(list))
					{
						double item2 = pole2D.CurBulge(node2D.Position);
						list2.Add(item2);
						if (list.Count > 1)
						{
							outPoleList.Add(list3);
						}
						this.DeleteNodeInClosedSpace(ref theNodes, list, list2);
						num = 1;
					}
					else
					{
						num = 0;
					}
					num2 = 0;
				}
				else
				{
					prevNode = null;
					isFirstPt = true;
					num2++;
					if (num2 == 2 && list.Count > 1)
					{
						outPoleList.Add(list3);
					}
				}
			}
			return true;
		}

		private bool SearchCells(List<Node2D> theNodes, ref List<List<Pole2D>> outPoleList)
		{
			int num = -1;
			int num2 = 0;
			int num3 = 0;
			int num4 = theNodes.Count * 4;
			bool isFirstPt = true;
			List<XYZ> list = new List<XYZ>();
			List<double> list2 = new List<double>();
			List<Pole2D> list3 = null;
			Node2D node2D = null;
			Node2D prevNode = null;
			while (theNodes.Count > 1 && num2 < 2 && num3 < num4)
			{
				if (num == -1 || num == 1)
				{
					list = new List<XYZ>();
					list2 = new List<double>();
					list3 = new List<Pole2D>();
					Node2D leftBottomNode = this.GetLeftBottomNode(theNodes);
					if (leftBottomNode == null)
					{
						return false;
					}
					node2D = leftBottomNode;
					list.Add(node2D.Position);
					num3++;
					prevNode = null;
					isFirstPt = true;
				}
				Pole2D nextEdge = null;
				Node2D nextNode = this.GetNextNode(isFirstPt, prevNode, node2D, theNodes, Node2D.SearchType._Clockwise, ref nextEdge);
				if (nextNode != null)
				{
					int num5 = list3.FindIndex((Pole2D value) => value.SameAs(nextEdge));
					if (num5 == -1)
					{
						double item = nextEdge.CurBulge(node2D.Position);
						list2.Add(item);
						list3.Add(new Pole2D(nextEdge));
					}
					prevNode = node2D;
					isFirstPt = false;
					node2D = nextNode;
					list.Add(node2D.Position);
					num3++;
					if (this.IsClosed(list))
					{
						double item2 = nextEdge.CurBulge(node2D.Position);
						list2.Add(item2);
						if (list.Count > 1)
						{
							outPoleList.Add(list3);
						}
						this.DeletePassNode(ref theNodes, list);
						this.RecursiveDeleteSingleNode(ref theNodes);
						num = 1;
					}
					else
					{
						num = 0;
					}
					num2 = 0;
				}
				else
				{
					prevNode = null;
					isFirstPt = true;
					num2++;
					if (num2 == 2 && list.Count > 1)
					{
						outPoleList.Add(list3);
					}
				}
			}
			return true;
		}

		private Node2D GetLeftBottomNode(List<Node2D> theNodes)
		{
			XYZ xyz = theNodes[0].Position;
			Node2D result = null;
			foreach (Node2D node2D in theNodes)
			{
				XYZ position = node2D.Position;
				if (Geometry.IsEqual(position.DistanceTo(xyz), 0.0, 0.0001))
				{
					result = node2D;
				}
				else if (this.CompareXYLess(position, xyz))
				{
					xyz = position;
					result = node2D;
				}
			}
			return result;
		}

		private bool CompareXYLess(XYZ lhv, XYZ rhv)
		{
			if (Geometry.IsEqual(lhv.Y, rhv.Y))
			{
				if (Geometry.LessThan(lhv.X, rhv.X))
				{
					return true;
				}
			}
			else if (Geometry.LessThan(lhv.Y, rhv.Y))
			{
				return true;
			}
			return false;
		}

		private Node2D GetNextNode(bool isFirstPt, Node2D PrevNode, Node2D CurNode, List<Node2D> theNodes, Node2D.SearchType searchType, ref Pole2D nextEdge)
		{
			Node2D result = null;
			if (isFirstPt)
			{
				List<Pole2D> poleList = CurNode.PoleList;
				double referenceStartAngle = 3.1416926535897933;
				poleList.Sort(new AngleComparerByNode(CurNode.Position, referenceStartAngle));
				int i = 0;
				int num = 0;
				while (i < poleList.Count)
				{
					Pole2D pole2D = poleList[i];
					double val = 0.0;
					pole2D.GetWinding(CurNode.Position, ref val);
					if (!Geometry.LessThan(val, 0.0))
					{
						break;
					}
					double val2 = 0.0;
					pole2D.GetAngle(CurNode.Position, ref val2);
					if (!Geometry.GreaterThan(val2, Math.PI) || !Geometry.Lessthan_Or_Equal(val2, 4.71238898038469))
					{
						break;
					}
					num++;
					i++;
				}
				int num2 = 0;
				if (num > 0)
				{
					num2 += num;
					if (num == poleList.Count)
					{
						num2--;
					}
				}
				nextEdge = poleList[num2];
				nextEdge.SetIsPositiveDirection(CurNode.Position);
				XYZ pos = nextEdge.CurEndPoint(CurNode.Position);
				result = this.FindNodeByPos(theNodes, pos);
			}
			else
			{
				int num3 = CurNode.FindLinkPoleWithOtherNode(PrevNode.Position, searchType);
				if (num3 != -1)
				{
					num3++;
					if (num3 == CurNode.PoleList.Count)
					{
						num3 = 0;
					}
					nextEdge = CurNode.PoleList[num3];
					nextEdge.SetIsPositiveDirection(CurNode.Position);
					XYZ pos2 = nextEdge.CurEndPoint(CurNode.Position);
					result = this.FindNodeByPos(theNodes, pos2);
				}
			}
			return result;
		}

		private Node2D FindNodeByPos(List<Node2D> theNodes, XYZ pos)
		{
			foreach (Node2D node2D in theNodes)
			{
				if (Geometry.IsEqual(pos.DistanceTo(node2D.Position), 0.0, 0.0001))
				{
					return node2D;
				}
			}
			return null;
		}

		private bool IsClosed(List<XYZ> BorderPos)
		{
			if (BorderPos.Count < 2)
			{
				return false;
			}
			XYZ xyz = BorderPos[0];
			XYZ xyz2 = BorderPos[BorderPos.Count - 1];
			return Geometry.IsEqual(xyz.DistanceTo(xyz2), 0.0, 0.0001);
		}

		private void DeleteNodeInClosedSpace(ref List<Node2D> theNodes, List<XYZ> points, List<double> bulges)
		{
			try
			{
				List<List<XYZ>> list = new List<List<XYZ>>();
				List<List<double>> list2 = new List<List<double>>();
				List<EdgeInfo> list3 = new List<EdgeInfo>();
				Geometry.ConvertToSimpleClosedPolylineAndSimpleCurve(points, bulges, ref list, ref list2, ref list3);
				List<List<XYZ>> list4 = new List<List<XYZ>>();
				for (int i = 0; i < list.Count; i++)
				{
					List<XYZ> item = new List<XYZ>();
					if (Geometry.GetFittingPolyline(list[i], list2[i], ref item, AssistFunc.mmToFeet(200.0)))
					{
						list4.Add(item);
					}
				}
				List<int> list5 = new List<int>();
				List<UV> list6 = new List<UV>();
				for (int i = 0; i < theNodes.Count; i++)
				{
					XYZ position = theNodes[i].Position;
					bool flag = false;
					for (int j = 0; j < list3.Count; j++)
					{
						EdgeInfo edgeInfo = list3[j];
						if (!Geometry.IsEqual(edgeInfo.StartPoint.DistanceTo(edgeInfo.EndPoint), 0.0, 0.0001))
						{
							if (edgeInfo.IsArc)
							{
								XYZ ptCenterOfArc = null;
								Geometry.GetCenterWithBulge(edgeInfo.StartPoint, edgeInfo.EndPoint, edgeInfo.Bulge, ref ptCenterOfArc);
								if (Geometry.Is_Point_OnSegment(edgeInfo.StartPoint, edgeInfo.EndPoint, ptCenterOfArc, XYZ.BasisZ, position, 1E-09))
								{
									list5.Add(i);
									list6.Add(new UV(position.X, position.Y));
									flag = true;
									break;
								}
							}
							else if (Geometry.Is_Point_OnSegment(edgeInfo.StartPoint, edgeInfo.EndPoint, position, 1E-09))
							{
								list5.Add(i);
								list6.Add(new UV(position.X, position.Y));
								flag = true;
								break;
							}
						}
					}
					if (!flag)
					{
						for (int j = 0; j < list4.Count; j++)
						{
							int num = Geometry.PointInPloygon(list4[j], position, 0.0001, 0.0001);
							if (num != -1)
							{
								list5.Add(i);
								list6.Add(new UV(position.X, position.Y));
								break;
							}
						}
					}
				}
				List<int> list7 = new List<int>();
				for (int i = 0; i < list6.Count; i++)
				{
					int num2 = list5[i];
					Node2D node2D = theNodes[num2];
					List<Pole2D> poleList = node2D.PoleList;
					XYZ position2 = node2D.Position;
					int k = 0;
					while (k < poleList.Count)
					{
						Pole2D pole2D = poleList[k];
						XYZ xyz = pole2D.CurEndPoint(position2);
						if (xyz == null)
						{
							k++;
						}
						else
						{
							UV pt2d = new UV(xyz.X, xyz.Y);
							int num3 = list6.FindIndex((UV s) => Geometry.IsEqual(s.DistanceTo(pt2d), 0.0, 0.0001));
							if (num3 == -1)
							{
								k++;
							}
							else
							{
								Node2D node2D2 = this.FindNodeByPos(theNodes, xyz);
								if (node2D2 == null)
								{
									k++;
								}
								else
								{
									poleList.RemoveAt(k);
									num3 = node2D2.FindLinkPoleWithOtherNode(position2, Node2D.SearchType._Clockwise);
									if (num3 != -1)
									{
										node2D2.RemoveEdge(num3);
									}
								}
							}
						}
					}
					if (poleList.Count == 0)
					{
						list7.Add(num2);
					}
					else
					{
						node2D.AnticlockwiseSortElements();
					}
				}
				for (int i = list7.Count - 1; i >= 0; i--)
				{
					theNodes.RemoveAt(list7[i]);
				}
			}
			catch (Exception)
			{
				theNodes.Clear();
			}
		}

		private void FormatPoleLoops(List<List<Pole2D>> outPoleList, ref List<List<Pole2D>> formatPoleLoops)
		{
			for (int i = 0; i < outPoleList.Count; i++)
			{
				List<Pole2D> list = outPoleList[i];
				for (int j = 0; j < list.Count - 1; j++)
				{
					Pole2D pole = list[j];
					int num = list.FindIndex(j + 1, (Pole2D s) => s == pole);
					if (num >= 0 && num != j)
					{
						List<Pole2D> range = list.GetRange(0, j);
						List<Pole2D> range2 = list.GetRange(j + 1, num - j - 1);
						List<Pole2D> collection = new List<Pole2D>();
						if (num < list.Count - 1)
						{
							collection = list.GetRange(num + 1, list.Count - (num + 1));
						}
						range.AddRange(collection);
						if (range.Count > 1)
						{
							outPoleList.Add(range);
						}
						if (range2.Count > 1)
						{
							outPoleList.Add(range2);
						}
						outPoleList.RemoveAt(i);
						i--;
						break;
					}
				}
			}
			List<Pole2D> list2 = new List<Pole2D>();
			foreach (List<Pole2D> list3 in outPoleList)
			{
				formatPoleLoops.Add(list3);
				list2.AddRange(list3);
			}
			this.FormatContinuousPoles(ref list2);
		}

		private void FormatContinuousPoles(ref List<Pole2D> continuousPoles)
		{
			List<Node2D> list = new List<Node2D>();
			this.InitData(continuousPoles, Node2D.SearchType._Anticlockwise, ref list);
			this.TrimAllNode(ref list);
		}

		private void GetLoopCurves(List<List<Pole2D>> outPoleList, StructureLoopSearcher.SearchEdgeLocation searchEdgeLocation, ref List<List<Curve>> edgeLoops)
		{
			foreach (List<Pole2D> loop in outPoleList)
			{
				List<Curve> item = new List<Curve>();
				this.GetLoopCurves(loop, searchEdgeLocation, ref item);
				edgeLoops.Add(item);
			}
		}

		private void GetLoopCurves(List<Pole2D> loop, StructureLoopSearcher.SearchEdgeLocation searchEdgeLocation, ref List<Curve> edgeLoop)
		{
			if (loop.Count == 2 && loop[0].SameAs(loop[1]))
			{
				return;
			}
			foreach (Pole2D pole in loop)
			{
				Curve item = null;
				if (this.ConvertToCurve(pole, searchEdgeLocation, ref item))
				{
					edgeLoop.Add(item);
				}
			}
		}

		private bool ConvertToCurve(Pole2D pole, StructureLoopSearcher.SearchEdgeLocation searchEdgeLocation, ref Curve curve)
		{
			XYZ xyz = XYZ.BasisZ;
			XYZ xyz2;
			XYZ xyz3;
			if (searchEdgeLocation == StructureLoopSearcher.SearchEdgeLocation.SEL_Center)
			{
				if (pole.IsPositiveDirection)
				{
					xyz2 = pole.StartPoint;
					xyz3 = pole.EndPoint;
				}
				else
				{
					xyz2 = pole.EndPoint;
					xyz3 = pole.StartPoint;
					xyz = -xyz;
				}
			}
			else if (searchEdgeLocation == StructureLoopSearcher.SearchEdgeLocation.SEL_Outside)
			{
				if (pole.IsPositiveDirection)
				{
					xyz2 = pole.PositiveStartPoint;
					xyz3 = pole.PositiveEndPoint;
				}
				else
				{
					xyz2 = pole.NegativeEndPoint;
					xyz3 = pole.NegativeStartPoint;
					xyz = -xyz;
				}
			}
			else if (pole.IsPositiveDirection)
			{
				xyz2 = pole.NegativeStartPoint;
				xyz3 = pole.NegativeEndPoint;
			}
			else
			{
				xyz2 = pole.PositiveEndPoint;
				xyz3 = pole.PositiveStartPoint;
				xyz = -xyz;
			}
			if (pole.IsArcPole)
			{
				XYZ center = pole.Center;
				XYZ xyz4 = Geometry.CalculatMidPoint(xyz2, xyz3, center, xyz);
				try
				{
					if (Geometry.LessThan(pole.Bulge, 0.0))
					{
						curve = Arc.Create(xyz3, xyz2, xyz4);
					}
					else
					{
						curve = Arc.Create(xyz2, xyz3, xyz4);
					}
					return true;
				}
				catch (Exception)
				{
					return false;
				}
			}
			try
			{
				curve = Line.CreateBound(xyz2, xyz3);
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		private void DeleteSingleSideLineByBound(XYZ passInPos, XYZ passOutPos, ref List<Node2D> CurNodeAry)
		{
			for (int i = 0; i < CurNodeAry.Count; i++)
			{
				Node2D node2D = CurNodeAry[i];
				for (int j = 0; j < node2D.PoleList.Count; j++)
				{
					Pole2D pole2D = node2D.PoleList[j];
					XYZ startPoint = pole2D.StartPoint;
					XYZ endPoint = pole2D.EndPoint;
					if ((Geometry.IsEqual(passInPos, startPoint) && Geometry.IsEqual(passOutPos, endPoint)) || (Geometry.IsEqual(passInPos, endPoint) && Geometry.IsEqual(passOutPos, startPoint)))
					{
						CurNodeAry[i].PoleList.RemoveAt(j);
					}
				}
			}
		}

		private void DeleteNodeByBound(int nodeIndex, ref List<Node2D> CurNodeAry)
		{
			List<KeyValuePair<XYZ, XYZ>> list = new List<KeyValuePair<XYZ, XYZ>>();
			for (int i = 0; i < CurNodeAry[nodeIndex].PoleList.Count; i++)
			{
				Pole2D pole2D = CurNodeAry[nodeIndex].PoleList[i];
				KeyValuePair<XYZ, XYZ> item = new KeyValuePair<XYZ, XYZ>(pole2D.StartPoint, pole2D.EndPoint);
				list.Add(item);
			}
			for (int i = 0; i < list.Count; i++)
			{
				XYZ key = list[i].Key;
				XYZ value = list[i].Value;
				this.DeleteSingleSideLineByBound(key, value, ref CurNodeAry);
			}
			CurNodeAry.RemoveAt(nodeIndex);
		}

		private void RecursiveDeleteSingleNode(ref List<Node2D> nodeInfos)
		{
			int num = 0;
			for (int i = 0; i < nodeInfos.Count; i++)
			{
				if (nodeInfos[i].PoleList.Count < 2)
				{
					this.DeleteNodeByBound(i, ref nodeInfos);
					i--;
					num++;
				}
			}
			if (num > 0)
			{
				this.RecursiveDeleteSingleNode(ref nodeInfos);
			}
		}

		private void DeletePassNode(ref List<Node2D> CurFloorNodeDate, List<XYZ> points)
		{
			List<int> list = new List<int>();
			List<int> list2 = new List<int>();
			for (int i = 0; i < points.Count - 1; i++)
			{
				XYZ xyz = points[i];
				for (int j = 0; j < CurFloorNodeDate.Count; j++)
				{
					XYZ position = CurFloorNodeDate[j].Position;
					if (xyz == position)
					{
						if (CurFloorNodeDate[j].PoleList.Count > 2)
						{
							list2.Add(j);
						}
						list.Add(j);
						break;
					}
				}
			}
			int count = list2.Count;
			if (count == 1)
			{
				int index = list2[0];
				int num = list.FindIndex((int value) => value == index);
				if (num != -1)
				{
					list.RemoveAt(num);
				}
			}
			else if (count >= 2)
			{
				int indexS = list2[0];
				int num2 = list.FindIndex((int value) => value == indexS);
				if (num2 != -1)
				{
					if (num2 == 0)
					{
						int indexE = list2[1];
						int num3 = list.FindIndex((int value) => value == indexE);
						if (num3 != -1)
						{
							if (num3 - num2 == 1)
							{
								XYZ position2 = CurFloorNodeDate[indexS].Position;
								XYZ position3 = CurFloorNodeDate[indexE].Position;
								this.DeleteSingleSideLineByBound(position2, position3, ref CurFloorNodeDate);
								this.DeleteSingleSideLineByBound(position3, position2, ref CurFloorNodeDate);
							}
							list.RemoveRange(num3, list.Count - num3);
							list.RemoveAt(num2);
						}
					}
					else
					{
						int indexE = list2[list2.Count - 1];
						int num4 = list.FindIndex((int value) => value == indexE);
						if (num4 != -1)
						{
							if (num4 == list.Count - 1)
							{
								list.RemoveRange(num2, num4 - num2 + 1);
							}
							else
							{
								list.RemoveRange(num2, num4 + 1 - num2 + 1);
							}
						}
					}
				}
			}
			if (list.Count != 0)
			{
				list.Sort();
				for (int i = list.Count - 1; i > -1; i--)
				{
					this.DeleteNodeByBound(list[i], ref CurFloorNodeDate);
				}
			}
		}

		private ExternalCommandData m_Revit;

		private Document m_Doc;

		private UIApplication m_uiApp;

		private bool m_ShowTempInfo;

		public enum SearchEdgeLocation
		{
			SEL_Inside,
			SEL_Center,
			SEL_Outside
		}
	}
}
